package com.crs.mapper;

import com.crs.entity.Review;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ReviewMapper {

    /**
     * 用户提交评价
     * @param review
     */
    void submitReview(Review review);

    /**
     * 根据父评论id查询要回复的记录
     * @param parentReviewId
     */
    @Select("select * from review where parent_review_id = #{parentReviewId}")
    Review findByParentReviewId(Long parentReviewId);

    /**
     * 查找是否已经回复过了
     * @param id
     */
    @Select("select count(*) from review where parent_review_id = #{id}")
    int existsByParentReviewId(Long id);

    /**
     * 插入回复信息
     * @param review1
     */
    @Insert("insert into review (borrow_id, resource_id, user_id, rating, comment," +
            " reply, reply_time, parent_review_id,status,create_time,update_time) VALUES (#{borrowId},#{resourceId}" +
            ",#{userId},#{rating},#{comment},#{reply},#{replyTime},#{parentReviewId},#{status},#{createTime},#{updateTime})")
    void insertReview(Review review1);

    /**
     * 更新点赞数量
     * @param reviewId
     */
    @Update("update review set like_count = like_count+1 where id = #{reviewId}")
    void updateLike(Long reviewId);

    /**
     * 根据评论id来查找评论
     * @param id
     * @return
     */
    @Select("select * from review where id = #{id}")
    Review findById(Long id);
}
